<?php
// 广告主充值管理
namespace Admin\Controller;
use Think\Controller;
class AdvertiserRechargeController extends Controller
{
	public function __construct($value='')
	{
		$this->isAjaxLogin();
	}
	//财务 充值
	public function rechargeAdd($value='')
	{
	 	
		$recharge_record	= M('recharge_record'); //充值记录
		$advertiser 		= M('advertiser a'); //广告主

		$advertiser_id 		= I('post.advertiser_id/d');//广告主id
		 
		$amount 			= I('post.amount/f');//充值金额

		$adv_data 	= $advertiser
					->join('wm_manage b on a.business_id=b.user_id')
					->field('a.contact_name,a.balance,a.business_id,a.company_name as adv_name,b.user_name as business_name,a.company_name')
				 	->where(['a.id' => $advertiser_id])
					->find();

		$balance   					=	round($adv_data['balance']+$amount,2);



		$user_info					=	session('user_info');

		$data['advertiser_name']	=	$adv_data['adv_name'];	//广告主名
		$data['advertiser_id']		=	$advertiser_id;//广告主id
		$data['recharge_amount']	=	$amount;///充值金额
		$data['recharge_time']		=	time();//充值时间
		$data['user_id']			=	$user_info['user_id'];//用户id
		$data['user_name']			=	$user_info['user_name'];//用户名
		$data['before_balance']		=	$adv_data['balance'];//充值前余额
		$data['balance']			=	$balance; //充值后余额
		$data['business_id']		=	$adv_data['business_id']; // 
		$data['business_name']		=	$adv_data['business_name']; // 
		$data['contact_name']		=	$adv_data['contact_name']; // 

		M()->startTrans();
		$res1=$recharge_record->add($data); 

		$res2=$advertiser
			->where(['id' => $advertiser_id])
			->setField('balance', $balance); //充值
		 
		if ($res1&&$res2) {
			M()->commit();
			$balance =	 $advertiser
					->where(['id' => $advertiser_id])
					->getField('balance');	//充值 后余额	
			$msg 		 			= 	'充值成功';
			$code 					= 	200;
			$return['balance']		=	$balance;
			$return['business_name']=   $adv_data['business_name'];
			$return['company_name']	=	$adv_data['company_name'];
			$return['contact_name']	=	$adv_data['contact_name'];

			$result=['msg' =>$msg,'code'=>$code,'data'=>$return];
		}else{
			M()->rollback();
			$result['msg']				= 	'充值失败';
			$result['code'] 			= 	500;
		}

		$this->ajaxReturn($result);
		 
	}


	//发票中心列表（搜索）
	public function rechargeList($value='')
	{
		$page   			=	I('get.page/d',1);
		$page_size 			=	I('get.page_size/d');
		$start_time			=	I('get.start_time/s');
		$end_time			=	I('get.end_time/s');
		$adv_id				=	I('get.adv_id/d');
		$invoice_status		=	I('get.invoice_status/s');//发票状态

		$where=[];

		if (C('IS_FALSE_DEL')) {
			$where['is_delete'] = '0';
		} 

        if(!empty($adv_id)&&($adv_id>-1)) {
            $where['advertiser_id'] = $adv_id;
        }

		if(in_array($invoice_status,['1','2','3','4'])) {
            $where['invoice_status'] = $invoice_status;
        }

        if (!empty($start_time)&&!empty($end_time)) {

        	$where['recharge_time']  = [['egt',$start_time-8*3600],['elt',$end_time+16*3600]]; 
        }

		$model=M('recharge_record')
			 ->field('id,recharge_time,advertiser_name,recharge_amount,invoice_status')
			 ->where($where);
		$res=my_page($model,$page,$page_size);

		// if (!empty($res['data'])) {

			$data=array_map(function($val){
				$val['recharge_time']=date("Y-m-d H:i:s",$val['recharge_time']);
				return $val;
			},$res['data']);
			$result['msg']='查询成功';
			$result['code']=200;
			 
		// }else{

			// $result['msg']='暂无数据';
			// $result['code']=500;
		// }

		$result['data']['total_page']=$res['count'];
		$result['data']['list']=$data;

		$this->ajaxReturn($result);
	}

	//充值记录列表
	public function rechargeRecord($value='')
	{
		$page   			=	I('get.page/d',1);
		$page_size 			=	I('get.page_size/d');
		$start_time			=	I('get.start_time/s');
		$end_time			=	I('get.end_time/s');
		$adv_id				=	I('get.adv_id/d');
	 
		$where=[];

		if (C('IS_FALSE_DEL')) {
			$where['is_delete'] = '0';
		} 
        if(!empty($adv_id)&&($adv_id>-1)) {
            $where['advertiser_id'] = $adv_id;
        }

        if (!empty($start_time)&&!empty($end_time)) {

        	$where['recharge_time']  = [['gt',$start_time-8*3600],['elt',$end_time+16*3600]]; 
        }

      
		$model=M('recharge_record')
			 ->field('id,recharge_time,advertiser_name,recharge_amount,invoice_status,balance,user_id,user_name,before_balance')
			 ->where($where);
		$res=my_page($model,$page,$page_size);

		// if (!empty($res['data'])) {

			$data=array_map(function($val){
				$val['recharge_time']=date("Y-m-d H:i:s",$val['recharge_time']);
				return $val;
			},$res['data']);
			$result['msg']='查询成功';
			$result['code']=200;
			$result['data']['total_page']=$res['count'];
			$result['data']['list']=$data;
			 
		// }else{

		// 	$result['msg']='暂无数据';
		// 	$result['code']=500;
		// 	$result['data']['total_page']=$res['count'];
		// 	$result['data']['list']=$data;
		// }

		$this->ajaxReturn($result);
	}
}